<?php

	if($_GET['operation']=="add" or $_GET['operation']=="edit")
	{
		if($_POST['act'])
		{	
			unset($_GET['operation']);
			unset($_GET['s']);
			unset($_GET['m']);

			//添加
			if($_POST["act"]=='save')
			{	
				if($_POST["pid"]==0)
				{
					$sql="select max(catid) as catid from ".PCATCUSTOMS." where catid<9999";
					$db->query($sql);
					$id=$db->fetchField("catid");
					if(!$id)
						$id=1000;
					else
						$id=substr($id*1000,0,4)+1;
				}
				else
				{
					$s=$_POST["pid"]."00";
					$b=$_POST["pid"]."99";
					$sql="select max(catid) as catid from ".PCATCUSTOMS." where catid>$s and catid<$b";
					$db->query($sql);
					$id=$db->fetchField("catid");
					if(!$id)
						$id=$_POST["pid"]."01";
					else
						$id=$id+1;
				}
				foreach(explode("\r\n",$_POST['cat']) as $catv)
				{
					if(!empty($catv))
					{
						$sql="insert into ".PCATCUSTOMS." (`catid`,`cat`,tax_rate,hscode,tax_paid_price,create_user) values 
							('$id','$catv','$_POST[tax_rate]','$_POST[hscode]','$_POST[tax_paid_price]','$_SESSION[ADMIN_USER]')";
						$db->query($sql);
						$id+=1;
					}
				}
			}
			//修改
			if($_POST["act"]=='edit' and is_numeric($_POST['id']))
			{
				
				if($_POST["pid"]!=substr($_POST['id'],0,strlen($_POST['id'])-2) and strlen($_POST['id'])>4 )
				{
					if($_POST["pid"]==0)
					{
						$sql="select max(catid) as catid from ".PCATCUSTOMS." where catid<9999";
						$db->query($sql);
						$id=$db->fetchField("catid");
						if(!$id)
							$id=1000;
						else
							$id=substr($id*1000,0,4)+1;
					}
					else
					{
						$s=$_POST["pid"]."00";
						$b=$_POST["pid"]."99";
						$sql="select max(catid) as catid from ".PCATCUSTOMS." where catid>$s and catid<$b";
						$db->query($sql);
						$id=$db->fetchField("catid");
						if(!$id)
							$id=$_POST["pid"]."01";
						else
							$id=$id+1;
					}
				}
				else
					$id=$_POST['id'];

				
				$sql="update ".PCATCUSTOMS." set catid='$id', cat='$_POST[cat]',tax_rate='$_POST[tax_rate]',hscode='$_POST[hscode]',tax_paid_price='$_POST[tax_paid_price]' where catid='".$_POST['id']."'";
				$db->query($sql);
				
				if($_POST["pid"]!=substr($_POST['id'],0,strlen($_POST['id'])-2))
				{
					//如果当前类别下面带有子类别把子类别一起移过去
					$s=$_POST['id']."00";
					$b=$_POST['id']."99";
					$sql="update ".PCATCUSTOMS." set catid=replace(catid,$_POST[id],$id) where catid like '$_POST[id]%'";
					$re=$db->query($sql);
				}
				unset($_GET['editid']);
			}
			$getstr=implode('&',convert($_GET));
			msg("?m=product&s=product_cat_customs.php&$getstr");
		}
		if($_GET['editid'] and is_numeric($_GET['editid']))
		{
			$sql="select * from ".PCATCUSTOMS." where catid='$_GET[editid]'";
			$db->query($sql);
			$re=$db->fetchRow();
			$re['cid']=substr($re['catid'],0,-2);
			$tpl->assign("re",$re);
		}
		
		$sql="select * from ".PCATCUSTOMS." where catid<9999 order by displayorder,catid";
		$db->query($sql);
		$de=$db->getRows();
		foreach($de as $key=>$val)
		{
			$sql="select * from ".PCATCUSTOMS." where catid < '".$val['catid']."99' and catid > '".$val['catid']."00' order by displayorder,catid";
			$db->query($sql);
			$a=$db->getRows();
			foreach($a as $ke=>$va)
			{
				$sql="select * from ".PCATCUSTOMS." where catid < '".$va['catid']."99' and catid > '".$va['catid']."00' order by displayorder,catid";
				$db->query($sql);
				$b = $db->getRows();
				foreach ($b as $bk=>$bv)
				{
				    $sql="select * from ".PCATCUSTOMS." where catid < '".$bv['catid']."99' and catid > '".$bv['catid']."00' order by displayorder,catid";
				    $db->query($sql);
				    $c = $db->getRows();
				    $b[$bk]['scat'] = $c;
				}
				$a[$ke]['scat']=$b;
			}
			$de[$key]['scat']=$a;
		}
	}
	else
	{
		if($_GET['delid'])
		{
			$sql="delete from ".PCATCUSTOMS."  where catid like '$_GET[delid]%' ";
			$db->query($sql);
			msg("?m=product&s=product_cat_customs.php");
		}
		if($_POST['act']=='op')
		{
			if($_POST['name'])
			{
				foreach($_POST['name'] as $key=>$list)
				{
					if(!empty($list))
					{
						$displayorder=$_POST['displayorder'][$key];
						$displayorder=$displayorder?$displayorder*1:"0";
						
						$db->query("update ".PCATCUSTOMS." set cat='$list',displayorder='$displayorder' where catid='$key'");		
					}
				}
			}
			msg("?m=product&s=product_cat_customs.php");
		}
	
		if($_GET['id'])
		{
			$id=$_GET['id']*1;
			
		}
		
			
		$sql="select * from ".PCATCUSTOMS." where 1 and hscode like '%000000%'  order by displayorder,catid";
		$db->query($sql);
		$de=$db->getRows();
		foreach($de as $k=>$v)
		{
		    //截取hocode前两位
		    $hscode_1 = substr($v['hscode'], 0,2);
			$tsql=" and hscode REGEXP '^".$hscode_1."..0000$' and hscode > '".$hscode_1."000000'";
			$sql="select * from ".PCATCUSTOMS." where 1 $tsql order by displayorder,catid";
			$db->query($sql);
			$a=$db->getRows();
			foreach($a as $ks=>$vs)
			{
			    //截取hocode前两位
			    $hscode_2 = substr($vs['hscode'], 0,4);
			    $tsql=" and hscode REGEXP '^".$hscode_2."..00$' and hscode > '".$hscode_2."0000' ";
				$sql="select * from ".PCATCUSTOMS." where 1 $tsql order by displayorder,catid";
				$db->query($sql);
				$b=$db->getRows();
				foreach($b as $kss=>$vss)
				{
				    //截取hocode前两位
				    $hscode_3 = substr($vss['hscode'], 0,6);
				    $tsql=" and hscode REGEXP '^".$hscode_2."..$' and hscode > '".$hscode_3."0000' ";
					$sql="select * from ".PCATCUSTOMS." where 1 $tsql order by displayorder,catid";
					$db->query($sql);
					$c=$db->getRows();
// 					foreach ($c as $ck=>$cv)
// 					{
// 					    //截取hocode前两位
// 					    $hscode_2 = substr($v['hscode'], 0,6);
// 					    $tsql=" and hscode REGEXP '^".$hscode_2."..$' ";
// 					    $sql="select * from ".PCATCUSTOMS." where 1 $tsql order by displayorder,catid";
// 					    $db->query($sql);
// 					    $d=$db->getRows();
// 					    $c[$ck]['scat']=$d;
// 					}
					$b[$kss]['scat']=$c;
						
				}
				$a[$ks]['scat']=$b;
				
			}
			$de[$k]['scat']=$a;
		}
	}

	$tpl->assign("de",$de);
	$tpl->display("product_cat_customs.htm");
?>
